﻿using Common;
using DB.Model;
using DB.Model.Input;
using DB.MongoDB;
using DotNetCore.CAP;
using Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NoticeWechat.Hangfire
{

    public class NodesJob
    {
        IMongoDbHelper<Nodes> _Db;
        IHttpClientHelper _IHttpClientHelper;

        public NodesJob(IMongoDbHelper<Nodes> mongoDb, IHttpClientHelper IHttpClientHelper)
        {
            _IHttpClientHelper = IHttpClientHelper;
            _Db = mongoDb;
        }

        public async Task<bool> Check_Nodes_HeartBeat()
        {
            //没有心跳的
            var Time = DateTime.Now.AddMinutes(-30);
            var list = _Db.AsQueryable().Where(x => x.Heartbeat < Time).ToList();

            foreach (var l in list)
            {
                PostMsg postMsg = new()
                {
                    Content = "失去心跳: " + l.Description,
                    FirstDate = DateTime.Now,
                    Received = l.Received,
                    TaskName = l.NodeName,
                    UserId = l.UserId
                };
                await _IHttpClientHelper.PostAsync(_IHttpClientHelper.GetHost() + "/tasks/PostMessage", postMsg.ToJson());
            }

            return true;
        }
    }
}
